﻿using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using Model;
using System.Collections.Generic;

namespace DAL
{
    public class Blog_UploadDAL
    {

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="extsql">sql条件</param>
        /// <param name="order">排序</param>
        /// <param name="pageIndex">页数</param>
        /// <param name="pageCount">条数</param>
        /// <param name="totalPages">总页数</param>
        /// <param name="totalItems">总条数</param>
        /// <returns></returns>
        public List<Blog_Upload> GetPage(string extsql, string order, int pageIndex, int pageCount, ref int totalPages, ref int totalItems)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(" select * from blog_upload where 1=1  ");
            sql.Append(extsql);
            if (!string.IsNullOrEmpty(order))
            {
                sql.Append(" order by " + order);
            }
            sql.Append(" limit " + ((pageIndex - 1) * pageCount) + "," + pageCount + ";");
            sql.Append("SELECT COUNT(*) FROM  blog_upload where 1=1 " + extsql + ";");

            return DBUtility.MySqlHelper.ExecuteDataReaderPage<Blog_Upload>(sql.ToString(), pageCount, ref totalPages, ref totalItems);
        }

        /// <summary>
        /// 获取一条数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Blog_Upload GetModel(string id)
        {
            return DBUtility.MySqlHelper.ExecuteDataReaderModel<Blog_Upload>("select * from blog_upload where ul_id=@id", new MySqlParameter("@id",id));
        }

        /// <summary>
        /// 获取自定义数据
        /// </summary>
        /// <param name="columns">列名</param>
        /// <param name="where">条件 </param>
        /// <returns></returns>
        public List<dynamic> GetList(string column, string where)
        {
            string sql = string.Format("select {0} from blog_upload where 1=1 {1}", column, where);
            return DBUtility.MySqlHelper.ExecuteDynamic(sql);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(Blog_Upload model)
        {
            MySqlParameter[] para = {
                new MySqlParameter("@ul_id",model.ul_id),
                new MySqlParameter("@ul_memid",model.ul_memid),
                new MySqlParameter("@ul_size",model.ul_size),
                new MySqlParameter("@ul_name",model.ul_name),
                new MySqlParameter("@ul_sourcename",model.ul_sourcename),
                new MySqlParameter("@ul_mimetype",model.ul_mimetype),
                new MySqlParameter("@ul_addtime",model.ul_addtime)
            };
            return DBUtility.MySqlHelper.ExecuteNonQuery(" insert into blog_upload (ul_id,ul_memid,ul_size,ul_name,ul_sourcename,ul_mimetype,ul_addtime) Values(@ul_id,@ul_memid,@ul_size,@ul_name,@ul_sourcename,@ul_mimetype,@ul_addtime)", para);
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int delete(string id)
        {
            return DBUtility.MySqlHelper.ExecuteNonQuery("delete from blog_upload where ul_id=@id", new MySqlParameter("@id", id));
        }

    }
}
